package tests;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import junit.framework.TestCase;


public class DeTodoUnPocoTest extends TestCase {
	private Connection db;
	private int nOfCalls, currentCall;
	
	public void setUp()  {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			this.db=DriverManager.getConnection("jdbc:mysql://alarcosj.esi.uclm.es:3306/bancoupm", "alumnodis", "alumnodis");
		} catch (Exception e) {
			e.printStackTrace();
		}
//		try {
//			DataOutputStream dos=new DataOutputStream(new FileOutputStream("/private/var/folders/59/5rlw8d8s2p37hv3d7dz0q3qr0000gn/T/counter.counter"));
//			dos.writeInt(1);
//			dos.close();
//			this.currentCall=5;
//			DataInputStream dis=new DataInputStream(new FileInputStream("/var/folders/59/5rlw8d8s2p37hv3d7dz0q3qr0000gn/T/" +
//					"int java.io.InputStream.read(byte[], int, int).jp"));
//			this.nOfCalls=dis.readInt();
//			System.out.println(nOfCalls);
//			dis.close();
//		}
//		catch (Exception ex) {
//			ex.printStackTrace();
//		}
	}

	public void testGetNumberOfOperations() {
		try {
			int nom=0;
			String SQL="Select count(*) from Movimiento";
			PreparedStatement p=this.db.prepareStatement(SQL);
			ResultSet r=p.executeQuery();
			if (r.next()) {
				nom=r.getInt(1);
			}
			this.db.close();
			System.out.println("nom=" + nom);
			assertTrue("Expected 11; obtained=" + nom, nom==11);
		} catch (Exception e) {
			fail("Unexpected exception: " + e.toString());
		}
	}

	public void testGetAmounts() {
		try {
			Vector<Double> amounts=new Vector<Double>();
			String SQL="Select Importe from Movimiento";
			PreparedStatement p=this.db.prepareStatement(SQL);
			ResultSet r=p.executeQuery();
			while (r.next()) {
				amounts.add(r.getDouble(1));
			}
			this.db.close();
			System.out.println("amounts.size()=" + amounts.size());
			assertTrue("Expected 11; obtained " + amounts.size(), amounts.size()==11);
		} catch (Exception e) {
			fail("Unexpected exception: " + e.toString());
		}
	}

		/**************************/
	
	private String fileName="/Users/Maco/Desktop/pruebasSeg/file1.txt";
	private String text1="One two three";
	private String text2="Four five six";

	public void testWriteString() {
		try {
			FileOutputStream f=new FileOutputStream(this.fileName);
			f.write(this.text1.getBytes());
			f.write(this.text2.getBytes());
			f.close();
			FileInputStream fi=new FileInputStream(this.fileName);
			byte[] b=new byte[fi.available()];
			fi.read(b);
			fi.close();
			String read=new String(b);
			assertTrue("Esperado: " + this.text1 + this.text2 + "; obtenido: " + read, read.equals(this.text1 + this.text2));
		} catch (IOException e) {
			fail("Not expected: " + e.toString());
		}
	}

	public void testWriteStringIntInt() {
		try {
			FileOutputStream f=new FileOutputStream(this.fileName);
			f.write(this.text1.getBytes(), 0, this.text1.length()-1);
			f.write(this.text2.getBytes(), 0, this.text2.length()-1);
			f.close();
			FileInputStream fi=new FileInputStream(this.fileName);
			byte[] b=new byte[fi.available()];
			fi.read(b);
			fi.close();
			String readed=new String(b);
			assertTrue("Esperado: " + this.text1 + this.text2 + "; obtenido: " + readed, readed.equals(this.text1 + this.text2));
		} catch (IOException e) {
			fail("Not expected: " + e.toString());
		}
	}


}
